package org.dromara.web.controller.platform;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.open.bean.result.WxOpenResult;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.platform.domain.bo.TEnterpriseInfoBo;
import org.dromara.platform.domain.vo.TEnterpriseInfoVo;
import org.dromara.platform.service.ITEnterpriseInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业信息
 *
 * @author Lion Li
 * @date 2025-05-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/platform/enterpriseInfo")
public class TEnterpriseInfoController extends BaseController {

    private final ITEnterpriseInfoService tEnterpriseInfoService;


    /**
     * 查询企业信息列表
     */
    @SaCheckPermission("platform:enterpriseInfo:list")
    @GetMapping("/list")
    public TableDataInfo<TEnterpriseInfoVo> list(TEnterpriseInfoBo bo, PageQuery pageQuery) {
        return tEnterpriseInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出企业信息列表
     */
    @SaCheckPermission("platform:enterpriseInfo:export")
    @Log(title = "企业信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TEnterpriseInfoBo bo, HttpServletResponse response) {
        List<TEnterpriseInfoVo> list = tEnterpriseInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "企业信息", TEnterpriseInfoVo.class, response);
    }

    /**
     * 获取企业信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("platform:enterpriseInfo:query")
    @GetMapping("/{id}")
    public R<TEnterpriseInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tEnterpriseInfoService.queryById(id));
    }

    /**
     * 新增企业信息
     */
    @SaCheckPermission("platform:enterpriseInfo:add")
    @Log(title = "企业信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TEnterpriseInfoBo bo) {
        return toAjax(tEnterpriseInfoService.insertByBo(bo));
    }

    /**
     * 修改企业信息
     */
    @SaCheckPermission("platform:enterpriseInfo:edit")
    @Log(title = "企业信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TEnterpriseInfoBo bo) {
        return toAjax(tEnterpriseInfoService.updateByBo(bo));
    }

    /**
     * 删除企业信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("platform:enterpriseInfo:remove")
    @Log(title = "企业信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tEnterpriseInfoService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 注册小程序
     */
    @PostMapping("/registerMiniprogram")
    @ResponseBody
    @RepeatSubmit
    public R registerMiniprogram(@RequestBody TEnterpriseInfoBo enterpriseInfoBo){
        return tEnterpriseInfoService.register(enterpriseInfoBo);
    }
}
